package com.school.service.impl;

import com.school.entity.Attendance;
import com.school.repository.AttendanceRepository;
import com.school.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

@Service
public class AttendanceServiceImpl implements AttendanceService {
    @Autowired
    private AttendanceRepository attendanceRepository;

    @Override
    public List<Attendance> findAll() {
        return attendanceRepository.findAll();
    }

    @Override
    public Optional<Attendance> findById(Long id) {
        return attendanceRepository.findById(id);
    }

    @Override
    public Attendance save(Attendance attendance) {
        return attendanceRepository.save(attendance);
    }

    @Override
    public void deleteById(Long id) {
        attendanceRepository.deleteById(id);
    }

    @Override
    public List<Attendance> findByClassId(Long classId) {
        return attendanceRepository.findByClassId(classId);
    }

    @Override
    public List<Attendance> findByClassIdAndDate(Long classId, LocalDate date) {
        return attendanceRepository.findByClassIdAndDate(classId, date);
    }

    @Override
    public List<Attendance> findByStudentId(String studentId) {
        return attendanceRepository.findByStudentId(studentId);
    }

    @Override
    public List<Attendance> findByDate(LocalDate date) {
        return attendanceRepository.findByDate(date);
    }

    @Override
    public void saveAll(List<Attendance> list) {
        attendanceRepository.saveAll(list);
    }

    @Override
    public void deleteByStudentId(String studentId) {
        List<Attendance> attendances = attendanceRepository.findByStudentId(studentId);
        if (!attendances.isEmpty()) {
            attendanceRepository.deleteAll(attendances);
        }
    }
}
